<?php
/**
 * Created by PhpStorm.
 * User: 王泽锋
 * Date: 2018/03/02
 * Time: 13:15
 */
namespace app\admin\controller;

use app\admin\model\Shopping_order as Shopping_orderModel;
use app\admin\model\Shopping_order_record as Shopping_order_recordModel;
use app\admin\model\Picture;
use think\Db;
/**
 * Class Order
 * @package 订单管理   控制器
 */
class Order extends Admin {
    /**
     * 订单主页
     */
    public function index(){
        $map = array(
            'status' => array('eq',0),
        );
        $list = $this->lists('Shopping_order',$map);
        int_to_string($list,array(
            'status' => array(0 =>"已下单"),
        ));

        $this->assign('list',$list);
        return $this->fetch();
    }

//    /**
//     * 订单添加
//     */
//    public function add(){
//        if(IS_POST) {
//            $data = input('post.');
//            $data['create_user'] = $_SESSION['think']['user_auth']['id'];
//            if(empty($data['id'])){
//                unset($data['id']);
//            }
//            $Shopping_orderModel = new Shopping_orderModel();
//            $info = $Shopping_orderModel->validate('Order')->save($data);
//            if($info) {
//                return $this->success("添加成功",Url('Order/index'));
//            }else{
//                return $this->error($Shopping_orderModel->getError());
//            }
//        }else{
//            $this->assign('msg','');
//            return $this->fetch('edit');
//        }
//    }
//
//    /**
//     * 订单修改
//     */
//    public function edit(){
//        if(IS_POST) {
//            $data = input('post.');
//            $Shopping_orderModel = new Shopping_orderModel();
//            $info = $Shopping_orderModel->validate('Order')->save($data,['id'=>input('id')]);
//            if($info){
//                return $this->success("修改成功",Url("Order/index"));
//            }else{
//                return $this->get_update_error_msg($Shopping_orderModel->getError());
//            }
//        }else{
//            $id = input('id');
//            $msg = Shopping_orderModel::get($id);
//            $this->assign('msg',$msg);
//            return $this->fetch();
//        }
//    }
//
//    /**
//     * 订单删除功能
//     */
//    public function del(){
//        $id = input('id');
//        $data['status'] = '-1';
//        $info = Shopping_orderModel::where('id',$id)->update($data);
//        if($info) {
//            return $this->success("删除成功");
//        }else{
//            return $this->error("删除失败");
//        }
//
//    }
//
    /**
     * 预览功能 
     * */
    public function preview() {
        $id=input('id');
        $data=Db::table('pb_shopping_order')->where('id',$id)->find();
        $list=Shopping_order_recordModel::where('oid',$data['id'])->select();
        foreach($list as $value){
            $value['front_cover'] = Picture::where('id',$value['front_cover'])->value('path');
        }
        
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 出货确认功能
     */
    public function shipment(){
        $id = input('id');
        $data['shipment'] = input('shipment');
        $info = Shopping_orderModel::where('id',$id)->update($data);
        if($info) {
            return $this->success("出货确认成功");
        }else{
            return $this->error("出货确认失败");
        }
    }



    /**
     * 随机X位数字函数
     * @param int $num
     *
     * @return string
     */
    public static function randNum($num = 4){
        $chars = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
        $charsLen = count($chars) - 1;
        shuffle($chars);
        $output = "";
        for ($i=0; $i<$num; $i++)
        {
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }


    /**
     * 根据用户ID生成订单编号
     * @param $id
     * @return string
     */
    public static function setOrderNo($id) {
        $num1 = date('Ymd');
        $num2 = substr(time(), -4);
        $randNum = self::randNum(4);
        if(strlen($id) < 4){
            $idNum = sprintf("%04d", $id);
        }elseif(strlen($id) > 4){
            $idNum = substr($id, -4);
        }else{
            $idNum = $id;
        }
        return $num1.$num2.$idNum.$randNum;
    }

}